package com;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author wdl
 */
@WebServlet(name = "OrderServlet")
public class OrderServlet extends HttpServlet {
    private static final String INSERT_OPERATION = "insert";
    private static final String QUERY_ALL_OPERATION = "queryAll";
    private static final String QUERY_BY_STATE_OPERATION = "queryByState";
    private static final String CANCEL_ORDER_OPERATION = "cancelOrder";

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //charset=utf-8使得可以显示中文
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //从客户端获取数据
        String operation = request.getParameter("operation");
        //获得数据库操作对象
        CarDB cardb = new CarDB();
        //结果返回客户端
        JSONObject object = new JSONObject();

        //需要使用到的一些变量
        String account;
        Order order;
        String carNumber;
        int result;
        //根据不同操作标识符进行不同操作
        switch (operation) {
            case INSERT_OPERATION:
                //订单的插入操作
                //从客户端获取数据
                account = request.getParameter("account");
                carNumber = request.getParameter("carNumber");
                String startDate = request.getParameter("startDate");
                String startTime = request.getParameter("startTime");
                String finishDate = request.getParameter("finishDate");
                String finishTime = request.getParameter("finishTime");

                System.out.println(account + carNumber + startDate + startTime + finishDate + finishTime);
                //创建实体类
                order = new Order(account, carNumber, startDate, startTime, finishDate, finishTime, 1);
                //插入数据0代表失败，1代表成功
                result = cardb.insertOrder(order);
                object.put("result", result);
                break;
            case QUERY_ALL_OPERATION:
            case QUERY_BY_STATE_OPERATION:
                //查询订单的操作
                //获取需要查询订单的账号
                account = request.getParameter("account");
                List<AppointOrder> appointOrderList;
                //如果是查询所有订单则查询所有订单
                if (operation.equals(QUERY_ALL_OPERATION)) {
                    appointOrderList = cardb.queryAppointOrderData(account);
                }
                //否者则查询对于状态的订单
                else {
                    //获取状态
                    int state = Integer.parseInt(request.getParameter("state"));
                    appointOrderList = cardb.queryOrderByState(account, state);
                }
                JSONArray data = new JSONArray();
                for (AppointOrder appointOrder :
                        appointOrderList) {
                    order = appointOrder.getOrder();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("name", appointOrder.getName());
                    jsonObject.put("tel", appointOrder.getTel());
                    jsonObject.put("carBrand", appointOrder.getCarBrand());
                    jsonObject.put("image", appointOrder.getImage());
                    jsonObject.put("freeTime", appointOrder.getFreeTime());
                    jsonObject.put("account", order.getAccount());
                    jsonObject.put("carNumber", order.getCarNumber());
                    jsonObject.put("startDate", order.getStartDate());
                    jsonObject.put("startTime", order.getStartTime());
                    jsonObject.put("finishDate", order.getFinishDate());
                    jsonObject.put("finishTime", order.getFinishTime());
                    jsonObject.put("state", order.getState());
                    data.add(jsonObject);
                }
                object.put("data", data);
                break;
            case CANCEL_ORDER_OPERATION:
                //取消订单的操作
                account = request.getParameter("account");
                carNumber = request.getParameter("carNumber");
                //取消订单0代表失败，1代表成功
                result = cardb.cancelAppointOrder(account, carNumber);
                object.put("result", result);
                break;
            default:
        }
        out.print(object);

        out.flush();
        out.close();
    }
}
